package com.wangcang.widgetlibrary.stacking;

import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.bean.SwipeCardBean;

import java.util.List;

/**
 * Created by zhenwang.xiang on 2017/8/23.
 * Description:
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyHolder> {
    private Context mContext;
    private List<SwipeCardBean> mDataList;

    public CardAdapter(Context context, List<SwipeCardBean> datalist) {
        mContext = context;
        mDataList = datalist;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_swipe_card, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.mName.setText(mDataList.get(position).getName());
        holder.mPrecent.setText(mDataList.get(position).getPostition() + "/" + mDataList.size());
        holder.mPhoto.setImageResource(mDataList.get(position).getImage());

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews = new SparseArray();
        private TextView mName;
        private TextView mPrecent;
        private ImageView mPhoto;

        public MyHolder(View itemView) {
            super(itemView);
            mName = (TextView) itemView.findViewById(R.id.tvName);
            mPrecent = (TextView) itemView.findViewById(R.id.tvPrecent);
            mPhoto = (ImageView) itemView.findViewById(R.id.iv);
        }
        public View getView(int viewId) {
            View view =mViews.get(viewId);
            if(view == null) {
                view = this.itemView.findViewById(viewId);
                this.mViews.put(viewId, view);
            }
            return view;
        }
        public void setAlpha(int viewId, float value) {
            if (Build.VERSION.SDK_INT >= 11) {
                this.getView(viewId).setAlpha(value);
            } else {
                AlphaAnimation alpha = new AlphaAnimation(value, value);
                alpha.setDuration(0L);
                alpha.setFillAfter(true);
                this.getView(viewId).startAnimation(alpha);
            }
        }
    }
}
